package class02_异或运算;

/**
 * 理论：
 *      1：任何数与0异或，都等于它本身
 *      2：任何数与自己相异或都等于0
 *      3：异或运算符合交换律  a^b = b^a
 * 问题：使用异或得方式交换两个变量得值(不声明另外的变量)
 */
public class Code01_Swap {

	public static void main(String[] args) {






		int a = 16;
		int b = 603;

		System.out.println(a);
		System.out.println(b);


		a = a ^ b;
		b = a ^ b;
		a = a ^ b;


		System.out.println(a);
		System.out.println(b);




		int[] arr = {3,1,100};

		int i = 0;
		int j = 0;

		arr[i] = arr[i] ^ arr[j];
		arr[j] = arr[i] ^ arr[j];
		arr[i] = arr[i] ^ arr[j];

		System.out.println(arr[i] + " , " + arr[j]);









		System.out.println(arr[0]);
		System.out.println(arr[2]);

		swap(arr, 0, 0);

		System.out.println(arr[0]);
		System.out.println(arr[2]);



	}


	public static void swap (int[] arr, int i, int j) {
		// arr[0] = arr[0] ^ arr[0];
		arr[i]  = arr[i] ^ arr[j];
		arr[j]  = arr[i] ^ arr[j];
		arr[i]  = arr[i] ^ arr[j];
	}



}
